<?php
session_start();
error_reporting(0);

include_once("func_general.php");
?>

<html>
<head>

<title>CMS | taylors.edu.my</title>

<link type="text/css" href="jq/themes/base/jquery.ui.all.css" rel="stylesheet" />
<link href="style1.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="jq/demos.css">
<link rel="stylesheet" href="../css/global1.css">

<script src="../script/global.js"></script>
<script src="../script/switchcontent.js"></script>
<script src="jq/jquery-1.4.2.js"></script>
<script src="jq/ui/jquery.ui.core.js"></script>
<script src="jq/ui/jquery.ui.widget.js"></script>
<script src="jq/ui/jquery.ui.button.js"></script>
<script src="jq/ui/jquery.ui.mouse.js"></script>
<script src="jq/ui/jquery.ui.tabs.js"></script>
<script src="jq/ui/jquery.ui.resizable.js"></script>
<script src="jq/ui/jquery.ui.draggable.js"></script>
<script src="jq/func_module.js"></script>
<script src="scripts/swfobject.js"></script>
<script src="tiny_mce/jquery.tinymce.js"></script>

<style type="text/css">
#tabs-2{height:30px}
.defaultbox{width:100px; height:100px; border:1px solid #999}
.demo{ position:absolute; top:90px; left:0px; width:1200px; height:800px; border:1px solid gray}
.property_panel{ position:fixed; bottom:0px; left:230px; width:1200px; height:300px; border:1px solid gray; padding:5px; background-color:#CCCCCC; overflow:auto; z-index:1100;}
.ui-resizable-helper { border: 1px dotted gray; }
</style>

<script type="text/javascript">

	var layout_id;
	var meta_id;
	var boxid = 0;
	var curr_boxId = '';
	var curr_l;
	var curr_t;
	var curr_w;
	var curr_h;
	var module_id;
	var boxid_arr = Array();
	var canvasW = 1200;
	var canvasH = 600;
	var canvasOffH = 360;
	var html_str = '';
	var css_str = '';
	var isMouseAdjust;
	
	
	function init(){
		
		initbox();
		
		$('#cW').val(canvasW);
		$('#cH').val(canvasH);
		
		var cssObj = {
			'width' : canvasW,
			'height' : Number(canvasH)+canvasOffH
		}
		$('#demo').css(cssObj);
	}
	
	function switchLang(){
		var langVal = $('#lang_dd').val();
		var param = "&meta_id=<? echo $_REQUEST['meta_id']; ?>";
		param += "&back_parentids=<? echo $_REQUEST['back_parentids']; ?>";
		param += "&title=<? echo urlencode($_REQUEST['title']); ?>";
		param += "&preview_link=<? echo $_REQUEST['preview_link'];?>";
		
		window.location.href = "edit_layout.php?lang=" + langVal + param;
	}

	function initbox(){
		var allSource = $('#tabs-1').find('textarea').text();
		allSource = allSource.replace(/block/gi, "tempbox");
		$('#storedata').html(allSource);
		var rawData = $('#boxdata').text();
		
		//alert("rawData: "+rawData);
		
		boxid_arr = [];
		
		if(rawData!=""){
			var dataArr = rawData.split("||");
			
			if(dataArr.length>=3){
				
				canvasW = dataArr[0];
				canvasH = dataArr[1];
				
				var boxDataArr = dataArr[2].split("|");
				
				var i=0;
				for(i=0;i<boxDataArr.length;i++){
					var infoArr = boxDataArr[i].split(",");
					if(infoArr.length>1){
						var demo_pos = $("#demo").position();
						var id = Number(infoArr[0]);
						//var l = Number(infoArr[1])+demo_pos.left;
						//var t = Number(infoArr[2])+demo_pos.top;
						var l = Number(infoArr[1]);
						var t = Number(infoArr[2]);
						var w = Number(infoArr[3]);
						var h = Number(infoArr[4]);
						var m_id = Number(infoArr[5]);
						var c_id = Number(infoArr[6]);
						var boxName = "block"+id;
						
						boxid_arr.push({'id': id, 'l':l, 't':t, 'w':w, 'h':h, 
							'module_id':m_id, 'content_id':c_id, 'source':''
						});
						boxid = Number(infoArr[0]);
					}
				}
				if(boxid_arr.length>0){
					//loop();
					updateSource();
					$("div.demo").html(html_str);
					updateBoxProp();
				}
			
			}
		}
		
		$('#storedata').html("");
		
	}
	
	function loadAllSource(){
		var i=0;
		while(i<boxid_arr.length){
			var boxName = "block" + boxid_arr[i]['id'];
			var c_id = boxid_arr[i]['content_id'];
			$.post("getContent.php", { content_id:c_id, box_id:boxName }, 
				function(data){
					var dataArr = data.split("^|^");
					var thisbox = dataArr[0];
					var s = dataArr[1];
					changeBoxSource(s, thisbox);
			});
			i++;
		}
	}
	
	
	function setCanvasDim(){
		canvasW = $('#cW').val();
		canvasH = $('#cH').val();
		
		var cssObj = {
			'width' : canvasW,
			'height' : Number(canvasH)+canvasOffH
		}
		$('#demo').css(cssObj);
		
		loop();
		//saveLayoutSource();
	}
	

	function addBox(){
		boxid++;
		var newBoxName = "block" + boxid;
		boxid_arr.push({'id': boxid, 'l':200, 't':200, 'w':100, 'h':100, 'source':'', 'module_id':0, 'content_id':0});
		
		loop();
		//saveLayoutSource();
	}
	
	function deleteBox(){
		var found=0;
		var i=0;
		var rid = 0;
		while(i<boxid_arr.length && found==0){
			var boxName = "block" + boxid_arr[i]['id'];
			if(curr_boxId==boxName){
				found = 1;
				rid = i;
				alert(boxName);
			}
			i++;
		}
		
		boxid_arr.splice(rid,1);
		loop();
		//saveLayoutSource();
	}
	
	function saveLayoutSource(){
		
		var source_str = $('#tabs-1').find('textarea').text();
		$.post("saveLayout.php", { layoutid:layout_id, metaid:meta_id, source:source_str }, 
			function(data){
				if(!layout_id){
					var info_arr = data.split("|");
					if(info_arr[0]=="0")layout_id = info_arr[1];
				}
				//alert(source_str);
				//alert(data);
		});
	}
	
	function updateBoxObj(){
		var i=0;
		for(i=0; i<boxid_arr.length;i++){
			var boxName = "block" + boxid_arr[i]['id'];
			var demo_pos = $("#demo").position();
			var pos = $('#'+boxName).position();
			//boxid_arr[i]['l'] = (pos.left<demo_pos.left) ? demo_pos.left : pos.left;
			//boxid_arr[i]['t'] = (pos.top<demo_pos.top) ? demo_pos.top : pos.top;
			boxid_arr[i]['l'] = pos.left;
			boxid_arr[i]['t'] = pos.top;
			boxid_arr[i]['w'] = $('#'+boxName).width();
			boxid_arr[i]['h'] = $('#'+boxName).height();
		}
	}
	
	function changeBoxContent(s, m_id, c_id, thisbox){
		var i=0;
		var enabled = true;
		while(i<boxid_arr.length && enabled){
			var boxName = "block" + boxid_arr[i]['id'];
			if(boxName==thisbox){
				boxid_arr[i]['source'] = s;
				boxid_arr[i]['module_id'] = m_id;
				boxid_arr[i]['content_id'] = c_id;
				var new_source = s.replace(/{ROOT_PATH}/gi, ROOT_PATH);
				$('#'+curr_boxId).html(new_source);
				enabled = false;
			}
			
			i++;
		}
		updateBoxProp();
		loop();
		updateBlockProperty();
		//saveLayoutSource();
	}
	
	function changeBoxSource(s, thisbox){
		var i=0;
		var enabled = true;
		while(i<boxid_arr.length && enabled){
			var boxName = "block" + boxid_arr[i]['id'];
			if(boxName==thisbox){
				boxid_arr[i]['source'] = s;
				var new_source = s.replace(/{ROOT_PATH}/gi, ROOT_PATH);
				$('#'+thisbox).html(new_source);
				enabled = false;
			}
			i++;
		}
		//loop();
		updateSource();
	}
	
	function getContentIDs(thisbox){
		var returnArr = [0,0];
		var i=0;
		var enabled = true;
		while(i<boxid_arr.length && enabled){
			var boxName = "block" + boxid_arr[i]['id'];
			if(boxName==thisbox){
				var m_id = boxid_arr[i]['module_id'];
				var c_id = boxid_arr[i]['content_id'];
				curr_l = boxid_arr[i]['l'];
				curr_t = boxid_arr[i]['t'];
				curr_w = boxid_arr[i]['w'];
				curr_h = boxid_arr[i]['h'];
				returnArr = [m_id, c_id];
				enabled = false;
			}
			i++;
		}
		
		return returnArr;
	}
	
	function updateBoxProp(){
		var temp_canvasH = canvasH;
		canvasH = 0;
		
		var i=0;
		for(i=0;i<boxid_arr.length;i++){
			
			var id = boxid_arr[i]['id'];
			var l = boxid_arr[i]['l'];
			var t = boxid_arr[i]['t'];
			var w = boxid_arr[i]['w'];
			var h = boxid_arr[i]['h'];
			var source = boxid_arr[i]['source'];
			var m_id = boxid_arr[i]['module_id'];
			var c_id = boxid_arr[i]['content_id'];
			var boxName = "block" + id;
			
			/*
			if(id=="3"){
				alert("block3 l: "+l+" | block3 t: "+t);
			}
			*/
			
			if(canvasH<t+h)canvasH = t+h;
			
			$('#cW').val(canvasW);
			$('#cH').val(canvasH);
			
			var canvas_cssObj = {
				'width' : canvasW,
				'height' : Number(canvasH)+canvasOffH
			}
			
			if(temp_canvasH!=canvasH && i>=boxid_arr.length-1){
				$('#demo').css(canvas_cssObj);
				updateSource();
			}
		
			var cssObj = {
				'position': 'absolute',
				'width' : w,
				'height' : h,
				'left' : l,
				'top': t,
				'border': '1px solid #666666',
				'vertical-align': 'top',
				'overflow': 'hidden',
				'padding': '0px'
			}
			
			if(source=="" && m_id!=0 && c_id!=0){
				$.post("getContent.php", { content_id:c_id, box_id:boxName }, 
					function(data){
						var dataArr = data.split("^|^");
						var thisbox = dataArr[0];
						var s = dataArr[1];
						//s = s.replace(/{ROOT_PATH}/gi, ROOT_PATH);
						//alert(s + ","+ROOT_PATH);
						changeBoxSource(s, thisbox);
						//loop();
				});
				
			}else{
				if($('#'+boxName).length){
					var new_source = source.replace(/{ROOT_PATH}/gi, ROOT_PATH);
					$('#'+boxName).html(new_source);
				}
			}
			
			if($('#'+boxName).length){
				
				$('#'+boxName).css(cssObj);
				
				//alert("updateBoxProp: " + boxName);
				
				$('#'+boxName).mouseup(function () {
					
					$(this).css("border","1px solid #FF0000");
					curr_boxId = $(this).attr('id');
					updateBlockProperty();
					updateBoxObj();
					loop();
					//saveLayoutSource();
					/*
					var ids_arr = getContentIDs(curr_boxId);
					var m_id = ids_arr[0];
					var c_id = ids_arr[1];
					
					
					
					updateBoxObj();
					loop();
					//saveLayoutSource();
					
					if(m_id!=0){
						
						$.post("getModuleList.php", { action: "property", id:m_id, content_id:c_id, currbox:curr_boxId},
						   function(data){
								var htmlSrc = '<span>'+curr_boxId+'</span> ';
								htmlSrc += getBlockAttr();
								htmlSrc += data;
							 $("div.property_panel").html(htmlSrc);
						   });
						
					}else{
						
						$.post("getModuleList.php", { action: "list"},
						   function(data){
							   var htmlSrc = '<span>'+curr_boxId+'</span> ';
								htmlSrc += getBlockAttr();
							   htmlSrc += data;
							 $("div.property_panel").html(htmlSrc);
						   });
						
					}
					*/
					
				});
				
				$('#'+boxName).mouseover(function () {
					$(this).css("border","1px solid #FF0000");
				});
				
				$('#'+boxName).mouseout(function () {
					$(this).css("border","1px solid #666666");
				});
				
				if(isMouseAdjust){
					$('#'+boxName).draggable();
					$('#'+boxName).bind( "dragstop", function(event, ui) {
						updateBoxObj();
					});	
					
					$('#'+boxName).resizable();
					$('#'+boxName).bind( "resize", function(event, ui) {
						updateBoxObj();
					});				
					
				}
			}
		}
	}
	
	function updateBlockProperty(){
		//curr_boxId = $(this).attr('id');
		var ids_arr = getContentIDs(curr_boxId);
		var m_id = ids_arr[0];
		var c_id = ids_arr[1];
		
		/*
		updateBoxObj();
		loop();
		saveLayoutSource();
		*/
		
		if(m_id!=0){
			
			$.post("getModuleList.php", { action: "property", id:m_id, content_id:c_id, currbox:curr_boxId},
			   function(data){
					var htmlSrc = '<span>'+curr_boxId+'</span> ';
					htmlSrc += getBlockAttr();
					htmlSrc += data;
				 $("div.property_panel").html(htmlSrc);
			   });
			
		}else{
			
			$.post("getModuleList.php", { action: "list"},
			   function(data){
				   var htmlSrc = '<span>'+curr_boxId+'</span> ';
					htmlSrc += getBlockAttr();
				   htmlSrc += data;
				 $("div.property_panel").html(htmlSrc);
			   });
			
		}
	}
	
	function updateSource(){
		
		html_str = '';
		css_str = '<style type="text/css">';
		css_str += 'body{text-align:center; padding:0; margin:0}';
		css_str += '#container{position:relative; margin: 0 auto 50px; width:'+canvasW+'px; height:'+canvasH+'px; text-align:left}';
		css_str += '#canvas{position:absolute; margin: 0 auto; width:'+canvasW+'px; border:0px none;}';
		css_str += '#global_footer{position:relative; margin: 0px auto -45px; width:'+canvasW+'px; border:0px none;}';
		css_str += '#stickybar{position:relative; margin: 0px auto; width:100%; text-align:center; height:40px; border:0px none;}';
		
		var box_data = canvasW+'||'+canvasH+"||";
		var i=0;
		for(i=0; i<boxid_arr.length;i++){
			var boxName = "block" + boxid_arr[i]['id'];
			var demo_pos = $("#demo").position();
			//var l = boxid_arr[i]['l']-demo_pos.left;
			//var t = boxid_arr[i]['t']-demo_pos.top;
			var l = boxid_arr[i]['l'];
			var t = boxid_arr[i]['t'];
			var w = boxid_arr[i]['w'];
			var h = boxid_arr[i]['h'];
			var s = boxid_arr[i]['source'];
			var m_id = boxid_arr[i]['module_id'];
			var c_id = boxid_arr[i]['content_id'];
			
			html_str += '<div id="'+boxName+'">'+s+'</div>';
			css_str += '#'+boxName+"{position:absolute; border:0px none; left:"+l+"px; top:"+t+"px; width:"+w+"px; height:"+h+"px}";
			
			if(i>0)box_data += "|";
			box_data += boxid_arr[i]['id']+","+l+","+t+","+w+","+h+","+m_id+","+c_id;
			
		}
		
		css_str += '</style>';
		css_str += '</head><body>';
		
		//alert("updateSource "+css_str);
		
		var source_str = css_str;
		source_str += '<div id="container"><div id="canvas">';
		source_str += html_str;
		source_str += '<div class="boxdata" id="boxdata" style="display:none">' + box_data + '</div></div></div>';
		source_str += '<div id="global_footer"></div>';
		source_str += '<div id="stickybar"></div>';
		source_str += "</body>";
		
		$( "#tabs-1" ).find( "textarea" ).text(source_str);
		saveLayoutSource();
	}
	
	function loop(){
		
		updateSource();
		$("div.demo").html(html_str);
		updateBoxProp();
		
	}
	
	function changeBlockAttr(){
		curr_l = $('#lnum').val();
		curr_t = $('#tnum').val();
		curr_w = $('#wnum').val();
		curr_h = $('#hnum').val();
		
		
		var i=0;
		var enabled = true;
		while(i<boxid_arr.length && enabled){
			var boxName = "block" + boxid_arr[i]['id'];
			if(boxName==curr_boxId){
				boxid_arr[i]['l'] = Number(curr_l);
				boxid_arr[i]['t'] = Number(curr_t);
				boxid_arr[i]['w'] = Number(curr_w);
				boxid_arr[i]['h'] = Number(curr_h);
				//alert("changeBlockAttr: "+boxName+" | " + curr_l +", "+ curr_t+", "+curr_w+", "+curr_h);
				enabled = false;
			}
			i++;
		}
		
		loop();
		//saveLayoutSource();
	}
	
	function getBlockAttr(){
		var htmlSrc = "";
		htmlSrc += '<span>x: </span><input type="text" class="input3" id="lnum" value="'+curr_l+'" />';
		htmlSrc += '<span>y: </span><input type="text" class="input3" id="tnum" value="'+curr_t+'" />';
		htmlSrc += '<span>w: </span><input type="text" class="input3" id="wnum" value="'+curr_w+'" />';
		htmlSrc += '<span>h: </span><input type="text" class="input3" id="hnum" value="'+curr_h+'" />';
		htmlSrc += '<input type="button" onClick="javaScript:changeBlockAttr();" value="update" />';
		htmlSrc += '<br>';
		
		return htmlSrc;
	}
	
	function changeModule(){
		module_id = $("#module_opt").attr('value');
		$.post("getModuleList.php", { action: "property", id: module_id, currbox: curr_boxId},
		   function(data){
			   var htmlSrc = '<span>'+curr_boxId+'</span> ';
			   htmlSrc += getBlockAttr();
			   htmlSrc += data;
			 $("div.property_panel").html(htmlSrc);
		   });
	}
	
	function toggleMouseAdjust(){
		isMouseAdjust = $("#mouseAdjust").attr('value');
		updateBoxProp();
	}
	
	function popitup(url) {
		newwindow=window.open(url,'_blank');
		if (window.focus) {newwindow.focus()}
		return false;
	}
	
	function initTemplateSelector(){
		$.post("getTemplateList.php", {action: "list"},
			   function(data){
				   var rawData = data;
				   var data_arr = rawData.split("|");

				   var i=0;
				   for(i=0;i<data_arr.length;i++){
					   var info_arr = data_arr[i].split(",");
					   var id = info_arr[0];
					   var name = info_arr[1];

					   $('#tpl_dd').append( new Option(name,id) );
				   }
				   
			   });
	}
	
	function inheritTemplate_in_action(tpl_src){
		//alert(tpl_src);
		var new_src = tpl_src;
		var i=0;
		var found = 0;
		var c_id;
		var c_h;
		while(i<boxid_arr.length && found==0){
			var boxName = "block" + boxid_arr[i]['id'];
			var m_id = boxid_arr[i]['module_id'];
			c_id = boxid_arr[i]['content_id'];
			c_h = boxid_arr[i]['h'];
			
			if(m_id=="1"){
				found = 1;
			}
			
			i++;
		}
		
		new_src = new_src.replace(/{C_ID}/gi, c_id);
		new_src = new_src.replace(/{C_H}/gi, c_h);
		
		
		$( "#tabs-1" ).find( "textarea" ).text(new_src);
		$('#storedata').html("");
		initbox();
		
		alert("template inherit successfully\n\n"+new_src);
		

	}
	
	function inheritTemplate(){
		var s_lbl = $("#tpl_dd :selected").text();
		var s_val = $("#tpl_dd :selected").val();
		
		$.post("getTemplateList.php", {action: "item", id:s_val},
			   function(data){
				   var tpl_source = data;
				   inheritTemplate_in_action(tpl_source);
			   });
	}
	
	$(document).ready(function() {
		$("#tabs").tabs();
		$( "button, input:submit, a", ".menu" ).button();
		initTemplateSelector();
		init();
	});

</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">



<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">

<tr align="left" valign="top">
<td width="220" height="100%">

    <table width="220" height="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td width="220" height="80" align="left" valign="top" style="overflow:hidden;">
		<?php include "header_menu1.php"; ?>
    </td>
    </tr>
    
    <tr>
    <td width="220" align="left" valign="top" style="overflow:hidden;">
		<?php
        $menuFile = "menu".$_SESSION['type'].".php";
        include $menuFile;	
        ?>
	</td>
    </tr>

    </table>

</td>

<?
	$meta_id = $_REQUEST['meta_id'];
	$lang = $_REQUEST['lang'];
	$lang_str = "en";
	
	if(isset($lang)){
		$sql = "SELECT * FROM param_lang WHERE id=".$lang;
		$result = mysql_query($sql, $link);
		if(mysql_num_rows($result)>0){
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$lang_str = $row['value'];
		}
	}
	
	$title = $_REQUEST['title'];
	$title = str_replace("\\","", $title);
	$pure_preview_link = $_REQUEST['preview_link'];
	$preview_link = $root_path . $lang_str .$pure_preview_link;
	
?>

<td width="100%" height="100%">

    <table border="0" width="100%" height="100%" cellpadding="0" cellspacing="0">
    <tr align="left" valign="top">
    
    <td class="form_login">
    <span class="title_txt">Layout Editor</span><br><br>
    
    <table cellpadding="0" cellspacing="0" width="800" border="0">
    <!--popitup-->
    
    <tr><td><span class="landing_title"><? echo $title; ?></span></td></tr>
    
    <tr><td>
    <?
	$lang_switcher = "<span class='link_blue'>Language Mode: </span>
	<select id='lang_dd' onChange='switchLang()'>";
	
	$lang_sql = "SELECT * FROM param_lang ORDER BY id asc";
	$lang_result = mysql_query($lang_sql, $link);
	if(mysql_num_rows($lang_result)>0){
		while($row=mysql_fetch_array($lang_result, MYSQL_ASSOC)){
			$id = $row['id'];
			$value = $row['value'];
			$select_attr = "";
			if($id==$lang) $select_attr = "selected=selected";
			$lang_switcher .= "<option value='{$id}' {$select_attr}>{$value}</option>";
		}
	}
	
	$lang_switcher .= "</select>";
	
	echo $lang_switcher;
	?>
    </td></tr>
    
    <tr><td><input type="button" value="Preview" onClick="javaScript: popitup('<? echo $preview_link; ?>');" /> | <span class="link_blue"><? echo $preview_link; ?></span><br><br></td></tr>
    <tr><td><a href="structure.php?action=listData&lang=<?php echo $lang; ?>&parent_ids=<?php echo $_REQUEST['back_parentids']; ?>"><span class="link_blue">back</span></a></td></tr>
    </table>
    
    <div class="main">
    
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Code</a></li>
            <li><a href="#tabs-2">Design</a></li>
        </ul>
        
    
    
    <div id="tabs-1">
    
    <?
        $action = $_REQUEST['action'];
        $back_parentids = $_REQUEST['back_parentids'];
		
        //
        switch($action){
            
            case "addnew":
                
                $meta_id = $_REQUEST['meta_id'];
                $source = $_REQUEST['source'];
                
                $query = "INSERT INTO tbl_layout ";
                $query .= " (meta_id,source,lang,draft)";
                $query .= " VALUE(".$meta_id.",'".$source."',".$lang.",0)";
                
                $result = mysql_query($query, $link);
                
                if($result){
                    $output = "sucessfully added";
                }else{
                    $output = "create failed ".mysql_error();
                }
                
                mysql_free_result($result);
                
                echo $output;	
            break;
            
            case "update":
                
                $id = $_REQUEST['id'];
                $source = $_REQUEST['source'];
                
                $query = "UPDATE tbl_layout SET source='".$source."'";
                $query .= " WHERE id=".$id;
				$query .= " AND draft=0";
				$query .= " AND lang=".$lang;
                
                $result = mysql_query($query, $link);
                
                if($result){
                    $output = "sucessfully updated";
                }else{
                    $output = "update failed ".mysql_error();
                }
                
                mysql_free_result($result);
                
                echo $output;	
            break;
        }
        
        $meta_id = $_REQUEST['meta_id'];
        
        $query = "SELECT * FROM tbl_layout";
        $query .= " WHERE meta_id=".$meta_id;
		$query .= " AND draft=0";
		$query .= " AND lang=".$lang;
        
        $result = mysql_query($query, $link);
        $total_row = mysql_num_rows($result);
        
        if($total_row>0){
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            $layout_id = $row['id'];
			$source = $row['source'];
			$source = str_replace("{ROOT_PATH}", $root_path, $source);
            $output = '<script>layout_id='.$layout_id.'; meta_id='.$meta_id.';</script>';
            $output .= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
            $output .= '<input type="hidden" name="action" value="update" />';
			$output .= '<input type="hidden" name="lang" value='.$lang.' />';
            $output .= '<input type="hidden" name="back_parentids" value="'.$back_parentids.'" />';
            $output .= '<input type="hidden" name="id" value="'.$layout_id.'" />';
            $output .= '<input type="hidden" name="meta_id" value="'.$row['meta_id'].'" />';
			$output .= '<input type="hidden" name="preview_link" value="'.$pure_preview_link.'" />';
            $output .= '<table cellpadding="0" cellspacing="0" width="800" border="0">';
            $output .= '<tr valign="top"><td><span class="input1">Source:</span></td><td><textarea id="source" name="source" class="input2">'.$source.'</textarea></td></tr>';
            $output .= '<tr><td></td><td><input type="submit" value="Save" /></td></tr></table></form>';
        
        }else{
            $output = '<script>meta_id='.$meta_id.';</script>';
            $output .= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
            $output .= '<input type="hidden" name="action" value="addnew" />';
			$output .= '<input type="hidden" name="lang" value='.$lang.' />';
            $output .= '<input type="hidden" name="back_parentids" value="'.$back_parentids.'" />';
            $output .= '<input type="hidden" name="meta_id" value="'.$meta_id.'" />';
			$output .= '<input type="hidden" name="preview_link" value="'.$pure_preview_link.'" />';
            $output .= '<table cellpadding="0" cellspacing="0" width="800" border="0">';
            $output .= '<tr valign="top"><td><span class="input1">Source:</span></td><td><textarea id="source" name="source" class="input2"></textarea></td></tr>';
            $output .= '<tr><td></td><td><input type="submit" value="Add New" /></td></tr></table></form>';
            
        }
        
        mysql_free_result($result);
        
        echo $output;	
    ?>
    
    <div id="storedata" style="display:none"></div>
    </div>
    
    <div id="tabs-2">
        <div id="menu" class="menu">
            <a href="JavaScript:addBox()">Add Block</a>
            <a href="JavaScript:deleteBox()">Delete Block</a> | 
            <span>canvas width</span><input type="text" name="cW" id="cW"> <span>canvas height</span><input type="text" name="cH" id="cH"><a href="javaScript:setCanvasDim()">Set Canvas Dimension</a>
            <input type="checkbox" name="mouseAdjust" id="mouseAdjust" onChange="javaScript:toggleMouseAdjust();" /><span>mouse adjustment</span>
            <span> | inherit template: </span><select id="tpl_dd" style="width:150px;"></select>
            <input type="button" id="inherit_btn" value="inherit" onClick="javaScript:inheritTemplate();" />
        </div>
        
        <br>
       
        <div id="demo" class="demo">
        
        </div>
       
        <div id="property_panel" class="property_panel">
        <span>Property</span>
        </div>
        
    </div>
    
    </div>
    
    </div>
    
    </td>
    </tr>
    </table>
    
</td>

</tr>

</table>

<?php
mysql_close($link);
?>

</body>
</html>